﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Hook.MenuAssets.Screens;

namespace Hook.MenuAssets
{
    public class Screen
    {
        public const double MOUSELOADTIME = .5;

        public double TransInTime, TransOutTime, TransTimer, MouseLoadTimer;
        public bool IsTransIn, IsTransOut;
        public int LevelChosen;
        public Screen NextScreen;

        public List<MenuPane> Panes;

        public Screen()
        {
            TransInTime = TransOutTime = 1;
            IsTransIn = IsTransOut = false;
            NextScreen = null;
            MouseLoadTimer = 0;

            Panes = new List<MenuPane>();
        }

        public void HandleInput()
        {
            if(!IsTransIn && !IsTransOut)
            {
                foreach(MenuPane MP in Panes)
                {
                    foreach(MenuItem MI in MP.PaneList)
                    {
                        if(MouseInBounds(MP, MI))
                        {
                            if(Mouse.GetState().LeftButton == ButtonState.Pressed && MouseLoadTimer == 0)
                            {
                                if(MI is ScreenButton)
                                {
                                    if (this is LevelSelectScreen && ((ScreenButton)MI).TextSprite.Text.Equals("DEBUG"))
                                    {
                                        LevelChosen = 0;
                                    }
                                    else if (this is LevelSelectScreen && ((ScreenButton)MI).TextSprite.Text.Equals("FOREST"))
                                    {
                                        LevelChosen = 1;
                                    }

                                    NextScreen = ((ScreenButton)MI).NextScreen;
                                    if(NextScreen == ScreenManager.BackScreen)
                                    {
                                        CueExit();
                                    }
                                }

                                MouseLoadTimer = MOUSELOADTIME;
                            }
                        }
                    }
                }
            }
        }

        public bool MouseInBounds(MenuPane MP, MenuItem MI)
        {
            float MouseX = Mouse.GetState().X - Game1.RESOLUTION.X / 2;
            float MouseY = Mouse.GetState().Y - Game1.RESOLUTION.Y / 2;
            Vector3 Origin = MP.Position + MI.Position;

            if(MouseX > Origin.X - MI.Dimension.X / 2 &&
                MouseX < Origin.X + MI.Dimension.X / 2 &&
                MouseY > Origin.Y - MI.Dimension.Y / 2 &&
                MouseY < Origin.Y + MI.Dimension.Y / 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void CueEntrance()
        {
            TransTimer = TransInTime;
            IsTransIn = true;
            NextScreen = null;
        }
        public void CueExit()
        {
            TransTimer = TransOutTime;
            IsTransOut = true;
        }

        public void Update(GameTime gt)
        {
            HandleInput();

            if(IsTransIn)
            {
                TransTimer -= gt.ElapsedGameTime.TotalSeconds;
                if(TransTimer <= 0)
                {
                    TransTimer = 0;
                    IsTransIn = false;
                }
            }
            if(IsTransOut)
            {
                TransTimer -= gt.ElapsedGameTime.TotalSeconds;
                if(TransTimer <= 0)
                {
                    TransTimer = 0;
                }
            }

            if(MouseLoadTimer > 0)
            {
                MouseLoadTimer -= gt.ElapsedGameTime.TotalSeconds;
                if(MouseLoadTimer <= 0)
                {
                    MouseLoadTimer = 0;
                }
            }

            foreach(MenuPane MP in Panes)
            {
                MP.Update(gt);
            }
        }
        public void Draw(SpriteBatch sb)
        {
            foreach(MenuPane MP in Panes)
            {
                MP.Draw(sb);
            }
        }
    }
}
